import { defineStore } from 'pinia'
import { msgTip } from '@/hooks/useMsgTip.js'

import useUIStore from '@/stores/UI.js'

const UIStore = useUIStore()
const { UIOnStart, UIOnComplete } = UIStore
export const useMultipleUIStore = defineStore('MultipleUIStore', () => {
  const btnData = ref({
    pop_size: {
      name: 'N',
      disabled: true,
      intoTable: false
    },
    var_dim: {
      name: 'D',
      disabled: true,
      intoTable: false
    },
    obj_dim: {
      name: 'M',
      disabled: true,
      intoTable: false
    }
    ,
    max_fun_eval: {
      name: 'FE',
      disabled: true,
      intoTable: false
    }
  })
  const formData = ref(null)
  const selectDisabled = ref(false)
  const excelDisabled = ref(true)
  const drawingBtnDisabled = ref(false)
  // 页面加载动画
  const loading = ref(null)
  const btnIntoTable = (name) => {
    btnData.value[name].intoTable = !btnData.value[name].intoTable
  }
  const UIOnMultipleSubmit = () => {
    UIOnStart()
    selectDisabled.value = true
    excelDisabled.value = true
    changeBtnState(true)
  }
  const changeBtnState = (state) => {
    for (const key in btnData.value) {
      btnData.value[key].disabled = state
    }
  }

  const onMultipleError= ()=>{
    UIOnComplete()
    selectDisabled.value = false
    excelDisabled.value = true
    drawingBtnDisabled.value = false
    if (loading.value != null){
      loading.value.close()
    }
  }

  const onMultipleComplete = () => {
    UIOnComplete()
    selectDisabled.value = false
    excelDisabled.value = false
    changeBtnState(false)
  }

  const UIOnMultipleComplete = () => {
    msgTip('Success', 'multiple task completed', 'success')
    onMultipleComplete()
  }

  const onExcelComplete = (message) => {
    ElMessage.success(message['tips'])
    excelDisabled.value = false
  }

  const onDrawingComplete = (message) => {
    ElMessage.success(message['tips'])
    drawingBtnDisabled.value = false
    loading.value.close()
  }

  return {
    btnData,
    btnIntoTable,
    UIOnMultipleSubmit,
    UIOnMultipleComplete,
    selectDisabled,
    onMultipleComplete,
    excelDisabled,
    onExcelComplete,
    drawingBtnDisabled,
    onDrawingComplete,
    loading,
    onMultipleError
  }
})
